<template>
  <!-- 送货管理 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <!-- <el-button
          type="primary"
          icon="el-icon-plus"
          @click="add"
          v-auth-button="['add/deliverManagement']"
        ></el-button> -->
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="searchValue.search"
            placeholder="客户名称"
            clearable
          ></el-input>
          <el-select
            v-model="searchValue.type"
            placeholder="请选择收货状态"
            style="width: 100%"
            clearable
          >
            <el-option label="待发货" :value="0" />
            <el-option label="已发货" :value="1" />
            <el-option label="已收货" :value="2" />
          </el-select>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        row-key="id"
        stripe
        @filterType="filterType"
        @row-dblclick="handleRowDblclick"
      >
        <!-- <el-table-column label="ID" prop="id" width="50"></el-table-column> -->
        <el-table-column
          label="合同名称"
          prop="contractName"
          min-width="250"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <!-- <el-table-column
          label="客户名称"
          prop="customerName"
          min-width="200"
        ></el-table-column> -->
        <!-- <el-table-column
          label="生产单"
          prop="produceName"
          min-width="200"
        ></el-table-column> -->
        <el-table-column label="配送人" prop="deliveryName" width="100">
        </el-table-column>
        <el-table-column label="配送时间" prop="deliverTime" width="150">
        </el-table-column>
        <el-table-column label="送货状态" prop="typeName" width="100">
          <template #default="scope">
            <!--0未发货 1已发货 2已收货 -->
            <p v-if="scope.row.type == 0">{{ scope.row.typeName }}</p>
            <p v-if="scope.row.type == 1" style="color: #dfaf14">
              {{ scope.row.typeName }}
            </p>
            <p v-if="scope.row.type == 2" style="color: #2bd814">
              {{ scope.row.typeName }}
            </p>
          </template>
        </el-table-column>
        <el-table-column label="来源" prop="source" width="100">
          <template #default="scope">
            <p v-if="scope.row.source == 0">生产单</p>
            <p v-if="scope.row.source == 1">退货单</p>
          </template>
        </el-table-column>
        <!-- <el-table-column label="收货地址" prop="address" min-width="250">
        </el-table-column> -->
        <!-- <el-table-column label="备注" prop="remark" min-width="200">
        </el-table-column> -->
        <!-- <el-table-column label="审核状态" prop="status" width="200" sortable>
          <template #default="scope"> -->
        <!-- 0未审核 1审核通过 2审核拒绝 3审核中  -->
        <!-- <p v-if="scope.row.status == 0">未审核</p>
            <p v-if="scope.row.status == 1" style="color: #67c23a">通过</p>
            <p v-if="scope.row.status == 2" style="color: #f56c6c">未通过</p>
            <p v-if="scope.row.status == 3">审核中</p>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" fixed="right" align="center" width="250">
          <template #default="scope">
            <el-button-group>
              <el-button
                v-if="scope.row.type === 0"
                text
                type="primary"
                size="small"
                @click="sendProducts(scope.row, scope.$index)"
                v-auth-button="['send/deliverManagement']"
                >发货</el-button
              >
              <el-popconfirm
                title="确定收到该订单的货物？"
                @confirm="compeletProduct(scope.row, scope.$index)"
              >
                <template #reference>
                  <el-button
                    v-if="scope.row.type === 1"
                    text
                    type="primary"
                    size="small"
                    v-auth-button="['compelet/deliverManagement']"
                    >确定收货</el-button
                  >
                </template>
              </el-popconfirm>

              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                v-auth-button="['show/deliverManagement']"
                >查看</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_showAll(scope.row, scope.$index)"
                >查看合同</el-button
              >
              <!-- <el-button
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                v-auth-button="['edit/deliverManagement']"
                >编辑</el-button
              > -->
              <!-- <el-popconfirm
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
              >
                <template #reference>
                  <el-button
                    text
                    type="primary"
                    size="small"
                    v-auth-button="['delete/deliverManagement']"
                    >删除</el-button
                  >
                </template>
              </el-popconfirm> -->
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <el-dialog v-model="dialogVisible" title="确定发货信息" width="30%">
    <el-form
      :model="sendForm"
      :rules="sendRules"
      ref="sendForm"
      label-width="auto"
      label-position="right"
    >
      <el-form-item label="配送时间" prop="deliverTime" style="width: 100%">
        <el-date-picker
          v-model="sendForm.deliverTime"
          type="datetime"
          placeholder="请选择配送时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          :disabled-date="disabledDate"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="收货地址" prop="address" style="width: 100%">
        <el-input
          v-model="sendForm.address"
          rows="3"
          clearable
          type="textarea"
          placeholder="请输入收货地址"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button
          type="primary"
          :loading="sendProductsLoading"
          @click="requestSendProducts"
        >
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
  <comfirn-compeletd
    ref="comfirnCompeletd"
    @success="handleSaveSuccess"
    @closed="dialog.comfirn = false"
  >
  </comfirn-compeletd>
</template>

<script>
import { queryTypeByDic } from '@/utils/getData'
import comfirnCompeletd from './components/comfirnCompeletd.vue'
export default {
  name: 'deliverManagement',
  components: {
    comfirnCompeletd
  },
  data () {
    return {
      dialog: {
        save: false,
        comfirn: false
      },
      dialogVisible: false,
      sendForm: {
        id: '',
        type: '',
        deliverTime: '',
        address: ''
      },
      sendRules: {
        address: [
          { required: true, message: '请输入收货地址' }
        ],
        deliverTime: [
          { type: 'date', required: true, message: '请选择配送时间', trigger: 'change', },
        ]
      },
      sendProductsLoading: false,
      apiObj: this.$API.produce.deliver.getDeliverList,
      searchValue: {
        search: null,
        type: ''
      }
    }
  },
  methods: {
    //添加
    add () {
      // this.dialog.save = true
      // this.$nextTick(() => {
      //   this.$refs.saveDialog.open()
      // })
      this.$router.push({
        path: '/produce/deliverManagement/deliverAdd',
        query: {
          mode: 'add'
        }
      })
    },
    //编辑
    table_edit (row) {
      let str = row.status == 3 ? '审核中' : '已通过审核'
      if (row.status !== 0) {
        this.$message.error(`送货${str}，不能编辑`)
        return
      }
      this.$router.push({
        path: '/produce/deliverManagement/deliverAdd',
        query: {
          mode: 'edit',
          id: row.id
        }
      })
    },
    //发货
    async sendProducts (row) {
      this.dialogVisible = true
      this.sendForm = {
        id: row.id,
        type: 1,
        deliverTime: '',
        address: ''
      }
      var res = await this.$API.produce.deliver.getAddress.get({ contractId: row.contractId })
      this.sendForm.address = res.data
      // var data = {
      //   id: row.id,
      //   type: 1
      // }
      // this.requestProductApi(data)
    },
    requestSendProducts () {
      this.sendProductsLoading = true
      this.$refs.sendForm.validate(async (valid) => {
        if (valid) {
          var res = await this.$API.produce.deliver.updateStatus.post(this.sendForm)
          if (res.code == 200) {
            this.dialogVisible = false
            this.$refs.table.refresh()
            this.$message.success("操作成功")
          }
        }
        this.sendProductsLoading = true
      })
    },
    //确定收货
    compeletProduct (row) {
      this.comfirn = true
      this.$refs.comfirnCompeletd.open(row)
      // var data = {
      //   id: row.id,
      //   type: 2
      // }
      // this.requestProductApi(data)
    },
    async requestProductApi (data) {
      var res = await this.$API.produce.deliver.updateStatus.post(data)
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("操作成功")
      }
    },
    //查看
    table_show (row) {
      this.$router.push({
        path: '/produce/deliverManagement/deliverDetail',
        query: {
          id: row.id
        }
      })
    },
    // 查看合同总详情
    table_showAll (row) {
      this.$router.push({
        path: '/contract/contractManagement/detailList',
        query: {
          id: row.contractId
        }
      })
    },
    //删除 
    async table_del (row) {
      let str = row.status == 3 ? '审核中' : '已通过审核'
      if (row.status !== 0) {
        this.$message.error(`送货${str}，不能删除`)
        return
      }
      let data = {
        id: row.id,
        projectId: row.projectId
      }
      var res = await this.$API.produce.deliver.deleteDeliver.post(data)
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("删除成功")
      }
    },
    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },

    //本地更新数据
    handleSaveSuccess (data, mode) {
      if (mode == 'add') {
        // this.$refs.table.refresh()
      } else if (mode == 'edit') {
        // this.$refs.table.refresh()
      }
      this.$refs.table.refresh()
    },

    // 处理送货状态
    filterType (data) {
      let tableData = data
      queryTypeByDic('deliverStatus').then(res => {
        let list = res
        tableData.forEach(item => {
          list.forEach(f_item => {
            if (item.type == f_item.value) {
              item.typeName = f_item.name
            }
          })
        })
      })
    },
    // 双击表格
    handleRowDblclick (row) {
      this.table_show(row)
    }
  }
}
</script>

<style></style>
